package com.example.testapp.data.dao

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.example.testapp.data.entity.Balance
import java.time.LocalDateTime

/**
 * 账单数据访问对象
 */
@Dao
interface BalanceDao {

    /**
     * 插入账单
     * @param balance 账单对象
     * @return 插入的ID
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(balance: Balance): Long

    /**
     * 更新账单
     * @param balance 账单对象
     */
    @Update
    fun update(balance: Balance)

    /**
     * 删除账单
     * @param balance 账单对象
     */
    @Delete
    fun delete(balance: Balance)

    /**
     * 软删除账单
     * @param id 账单ID
     * @param delAt 删除时间
     * @return 更新的行数
     */
    @Query("UPDATE balance SET del_flag = 1, del_at = :delAt, update_date = :delAt WHERE id = :id")
    fun softDelete(id: Long, delAt: LocalDateTime): Int

    /**
     * 恢复软删除的账单
     * @param id 账单ID
     * @param updateDate 更新时间
     * @return 更新的行数
     */
    @Query("UPDATE balance SET del_flag = 0, del_at = NULL, update_date = :updateDate WHERE id = :id")
    fun restore(id: Long, updateDate: LocalDateTime): Int

    /**
     * 根据ID查询账单
     * @param id 账单ID
     * @return 账单对象
     */
    @Query("SELECT * FROM balance WHERE id = :id AND del_flag = 0")
    fun findById(id: Long): Balance?

    /**
     * 根据ID查询账单（包括已软删除）
     * @param id 账单ID
     * @return 账单对象
     */
    @Query("SELECT * FROM balance WHERE id = :id")
    fun findByIdIncludeDeleted(id: Long): Balance?

    /**
     * 获取所有未删除的账单
     * @return 账单列表
     */
    @Query("SELECT * FROM balance WHERE del_flag = 0 ORDER BY bill_date DESC")
    fun findAll(): List<Balance>

    /**
     * 获取所有未删除的账单（LiveData返回）
     * @return 账单LiveData列表
     */
    @Query("SELECT * FROM balance WHERE del_flag = 0 ORDER BY bill_date DESC")
    fun findAllLive(): LiveData<List<Balance>>

    /**
     * 根据状态查询账单
     * @param status 处理状态
     * @return 账单列表
     */
    @Query("SELECT * FROM balance WHERE status = :status AND del_flag = 0 ORDER BY bill_date DESC")
    fun findByStatus(status: Int): List<Balance>

    /**
     * 根据日期范围查询账单
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 账单列表
     */
    @Query("SELECT * FROM balance WHERE bill_date BETWEEN :startDate AND :endDate AND del_flag = 0 ORDER BY bill_date DESC")
    fun findByDateRange(startDate: LocalDateTime, endDate: LocalDateTime): List<Balance>

    /**
     * 根据类型查询账单
     * @param type 账单类型
     * @return 账单列表
     */
    @Query("SELECT * FROM balance WHERE type = :type AND del_flag = 0 ORDER BY bill_date DESC")
    fun findByType(type: Int): List<Balance>

    /**
     * 更新账单状态
     * @param id 账单ID
     * @param status 新状态
     * @param updateDate 更新时间
     * @return 更新的行数
     */
    @Query("UPDATE balance SET status = :status, update_date = :updateDate WHERE id = :id")
    fun updateStatus(id: Long, status: Int, updateDate: LocalDateTime): Int
    
    /**
     * 按照分页查询账单
     * @param offset 偏移量
     * @param limit 限制数量
     * @return 账单列表
     */
    @Query("SELECT * FROM balance WHERE del_flag = 0 ORDER BY bill_date DESC LIMIT :limit OFFSET :offset")
    fun findWithPaging(offset: Int, limit: Int): List<Balance>
} 